/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.loveDemandRecord.service;

import com.fzhucc.modules.loveDemandRecord.domain.LoveDemandRecord;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.loveDemandRecord.repository.LoveDemandRecordRepository;
import com.fzhucc.modules.loveDemandRecord.domain.dto.LoveDemandRecordDto;
import com.fzhucc.modules.loveDemandRecord.domain.dto.LoveDemandRecordQueryCriteria;
import com.fzhucc.modules.loveDemandRecord.service.mapstruct.LoveDemandRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-06-12
**/
@Service
@RequiredArgsConstructor
public class LoveDemandRecordService {

    private final LoveDemandRecordRepository loveDemandRecordRepository;
    private final LoveDemandRecordMapper loveDemandRecordMapper;

    public Map<String,Object> queryAll(LoveDemandRecordQueryCriteria criteria, Pageable pageable){
        Page<LoveDemandRecord> page = loveDemandRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(loveDemandRecordMapper::toDto));
    }

    public List<LoveDemandRecordDto> queryAll(LoveDemandRecordQueryCriteria criteria){
        return loveDemandRecordMapper.toDto(loveDemandRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public LoveDemandRecordDto findById(Integer id) {
        LoveDemandRecord loveDemandRecord = loveDemandRecordRepository.findById(id).orElseGet(LoveDemandRecord::new);
        ValidationUtil.isNull(loveDemandRecord.getId(),"LoveDemandRecord","id",id);
        return loveDemandRecordMapper.toDto(loveDemandRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    public LoveDemandRecordDto create(LoveDemandRecord resources) {
        return loveDemandRecordMapper.toDto(loveDemandRecordRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(LoveDemandRecord resources) {
        LoveDemandRecord loveDemandRecord = loveDemandRecordRepository.findById(resources.getId()).orElseGet(LoveDemandRecord::new);
        ValidationUtil.isNull( loveDemandRecord.getId(),"LoveDemandRecord","id",resources.getId());
        loveDemandRecord.copy(resources);
        loveDemandRecordRepository.save(loveDemandRecord);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            loveDemandRecordRepository.deleteById(id);
        }
    }

    public void download(List<LoveDemandRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LoveDemandRecordDto loveDemandRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", loveDemandRecord.getUserId());
            map.put("需求id", loveDemandRecord.getDemandId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}